/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.toolkit.impl;

import java.awt.Container;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import javax.swing.JScrollPane;
import javax.swing.JViewport;

import org.apache.log4j.Logger;

import who.tmap.toolkit.MappingToolKit;

public class MappingToolKitBitmapHelper {
	
    private static Logger logger = Logger.getLogger(MappingToolKitBitmapHelper.class);
    
    private MappingToolKit mtk;
    private BufferedImage thematicMapBackground;
	
	public MappingToolKitBitmapHelper( MappingToolKit mtk ) {
		this.mtk =mtk;
	}
	
	public BufferedImage getThematicMapBackground() {
		return thematicMapBackground;
	}
	
	public void captureThematicMapBackground( Container container ) {
		
		JScrollPane sp =(JScrollPane)container.getParent().getParent();
		JViewport view = sp.getViewport();
		
		Rectangle rect =view.getBounds();
		
		thematicMapBackground = new BufferedImage( rect.width, rect.height, BufferedImage.TYPE_INT_RGB );
	    Graphics2D g2 = thematicMapBackground.createGraphics();
	    
	    mtk.setSelectedShapesHidden( true );
	    view.paint(g2);
	    mtk.setSelectedShapesHidden( false );
	    
	    g2.dispose();
	    
	    /*
	    // For debugging purpose only
	    try {
	        File file = new File("d:/temp/thematicMapBackground.png");
	        ImageIO.write( thematicMapBackground, "png", file);

	    } catch (IOException e) {
	    	logger.error(e);
	    }
	    */
	    
	    logger.debug( "Leaving captureThematicMapBackground" );	    
	}
	
	public void displayThematicMapBackground( Container container ) {
		
		JScrollPane sp =(JScrollPane)container.getParent().getParent();
		
		Graphics2D g2d =(Graphics2D)sp.getGraphics();

		g2d.drawImage( thematicMapBackground, sp.getInsets().left, sp.getInsets().top, null);
		
	    logger.debug( "Leaving displayThematicMapBackground" );	    
	}

}
